package com.rabbitmq.controller;

import com.rabbitmq.config.ConfirmConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @Auther: liyanhao
 * @Description: 发布确认高级
 * @Date: 2022/5/12 17:05
 * @Version: v1.0
 */
@Slf4j
@RestController
@RequestMapping("/confirm")
public class ProducerController {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    //发送消息
    @GetMapping("/sendMessage/{message}")
    public void sendMessage(@PathVariable String message){
        //把消息id存进去 可以在确认回调中取到
        CorrelationData correlationData = new CorrelationData("1");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME,
                ConfirmConfig.CONFIRM_QUEUE_ROUTINGKEY,message,correlationData);
        log.info("发送消息内容：" + message);

        //发送 confirm_exchange_name错误的消息
        CorrelationData correlationData2 = new CorrelationData("2");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME+"2",
                ConfirmConfig.CONFIRM_QUEUE_ROUTINGKEY,message,correlationData2);
        log.info("发送消息内容：" + message);

        //发送confirm_queue_routingkey 错误的消息
        CorrelationData correlationData3 = new CorrelationData("3");
        rabbitTemplate.convertAndSend(ConfirmConfig.CONFIRM_EXCHANGE_NAME,
                ConfirmConfig.CONFIRM_QUEUE_ROUTINGKEY+"2",message,correlationData2);
        log.info("发送消息内容：" + message);
    }
}
